﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TeamFoundation.Build;
using Microsoft.TeamFoundation.Build.Client;
using Microsoft.TeamFoundation.Common;
using ShahyadS.TFSBuildInspectionModel;
using Microsoft.VisualStudio.Shell;
using System.ComponentModel.Composition;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.VersionControl.Client;

namespace ShahyadS.VisualStudio
{
	[Export(typeof(IVisualStudioAdapter))]
	[ExportMetadata("Version","10.0")]
	public class VS2010Adapter : IVisualStudioAdapter
	{
		TfsTeamProjectCollection TeamProjectCollection;
		IBuildServer BuildServer;
		VersionControlServer VersionControlServer;
		Uri _TeamProjectCollectionUrl;
		public Uri TeamProjectCollectionUrl
		{
			get
			{
				return _TeamProjectCollectionUrl;
			}
			set
			{
				_TeamProjectCollectionUrl = value;
				TeamProjectCollection = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(_TeamProjectCollectionUrl);
				BuildServer = (IBuildServer)TeamProjectCollection.GetService(typeof(IBuildServer));
				VersionControlServer = TeamProjectCollection.GetService<VersionControlServer>();
			}
		}
		public void QueueNewBuild(Uri BuildDefinitionUri)
		{
			var buildDefinition = BuildServer.QueryBuildDefinitionsByUri(new Uri[] { BuildDefinitionUri }).FirstOrDefault();
			BuildServer.QueueBuild(buildDefinition);
			//datarow.InvokeAction("QueueBuild");
		}
		public void OpenBuildExplorer(string teamProject, string buildDefinitionName)
		{
			IVsTeamFoundationBuild VsTfBuild = (IVsTeamFoundationBuild)ServiceProvider.GlobalProvider.GetService(typeof(IVsTeamFoundationBuild));
			VsTfBuild.BuildExplorer.CompletedView.Show(teamProject, buildDefinitionName, null, Microsoft.TeamFoundation.Build.Controls.DateFilter.Today);
		}
		public void FindInTeamExplorer(string teamProject, string buildDefinitionName)
		{
			IVsTeamExplorer VsTeamExplorer = (IVsTeamExplorer)ServiceProvider.GlobalProvider.GetService(typeof(IVsTeamExplorer));
			var teamProjectUri = VersionControlServer.TryGetTeamProject(teamProject).ArtifactUri.ToString();
			VsTeamExplorer.SetCurrentServerAndProject(_TeamProjectCollectionUrl.ToString(), teamProject, teamProjectUri);
			//datarow.InvokeAction("Select");
		}
		public void EditBuildDefinition(Uri buildDefinitionUri)
		{
			IVsTeamFoundationBuild VsTfBuild = (IVsTeamFoundationBuild)ServiceProvider.GlobalProvider.GetService(typeof(IVsTeamFoundationBuild));
			VsTfBuild.DefinitionManager.OpenDefinition(buildDefinitionUri);
		}
		public void OpenBuildDetail(Uri buildUri)
		{
			IVsTeamFoundationBuild VsTfBuild = (IVsTeamFoundationBuild)ServiceProvider.GlobalProvider.GetService(typeof(IVsTeamFoundationBuild));
			VsTfBuild.DetailsManager.OpenBuild(buildUri);
		}
		public void ShowControllerManager()
		{
			IVsTeamFoundationBuild VsTfBuild = (IVsTeamFoundationBuild)ServiceProvider.GlobalProvider.GetService(typeof(IVsTeamFoundationBuild));
			VsTfBuild.ControllerManager.Show();
		}

	}
}
